package servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.io.IOException;
import java.sql.*;

import static java.lang.System.out;

@WebServlet("/UserServlet")
public class UserServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html.charset=UTF-8");
        //Timestamp dateNow=new Timestamp(System.currentTimeMillis());
        //Date reDate=new Date(System.currentTimeMillis());
        //if(reDate==="21:55"){
            //out.write(Integer.parseInt("function alertness(){alert('打卡时间还有五分钟截止');}"));
        //}
        //if(reDate>="22:00"){
            //out.write(Integer.parseInt("function alertness(){alert('打卡已经截止');}"));
        //}
        int ID = Integer.valueOf(req.getParameter("ID"));
        String name = req.getParameter("name");
        String sex = req.getParameter("sex");
        String college = req.getParameter("college");
        String province = req.getParameter("province");
        String city = req.getParameter("city");
        String temperature = req.getParameter("temperature");
        String return1 = req.getParameter("return");
        String question = req.getParameter("question");
        String confirm = req.getParameter("confirm");
        String date1= String.valueOf(req.getDateHeader("date"));
        Connection connection=null;
        Statement stmt=null;
        try{
            Class.forName("com.mysql.jdbc.Driver");
            String url="jdbc:mysql://localhost:3306/epidemic";
            String user="root";
            String pwd="WJHwenjiahui245";
            connection=DriverManager.getConnection(url,user,pwd);
            stmt =connection.createStatement();
            String sql ="insert into epidemic values("+ID+",'"+name+"','"+sex+"','"+college+"','"+province+"','"+city+"',"+temperature+",'"+return1+"','"+question+"','"+confirm+"')";
            int row =stmt.executeUpdate(sql);
            if(row!=0){
                resp.getWriter().print("添加成功");
            }else{
                resp.getWriter().print("添加失败");
            }
        }catch (ClassNotFoundException | SQLException e){
            e.printStackTrace();
        }finally {
            try {
                stmt.close();
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
